package com.qianzhou.eduUserBoot.controller;


import com.qianzhou.eduCommon.constant.Constants;
import com.qianzhou.eduCommon.dto.UserDTO;
import com.qianzhou.eduCommon.dto.UserRoleDTO;
import com.qianzhou.eduCommon.po.User;
import com.qianzhou.eduCommon.vo.PageInfoVO;
import com.qianzhou.eduCommon.vo.ResultVO;
import com.qianzhou.eduCommon.vo.RoleVO;
import com.qianzhou.eduCommon.vo.UserVO;
import com.qianzhou.eduUserBoot.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
 * @Author: qian.zhou
 * @Description: 用户接口
 * @Date Created in 2021-08-31-21:27
 * @Modified By:
 */
@Api("用户接口")
@RestController
@RequestMapping("user")
public class UserController {
    @Autowired
    private UserService userService;

    @ApiOperation("id获取用户信息")
    @RequestMapping("getOne/{id}")
    public User getOne(@PathVariable("id") Integer id) {
        return userService.getOne(id);
    }

    @RequestMapping("findAllUserByPage")
    public ResultVO findAllUserByPage(@RequestBody UserDTO dto) {
        PageInfoVO<User> userPageInfoVO = userService.findAllUserByPage(dto);
        return ResultVO.ok(Constants.SUCCESS_STATUS, Constants.SUCCESS, userPageInfoVO);
    }

    @RequestMapping("updateUserStatus")
    public ResultVO updateUserStatus(@RequestBody UserDTO dto) {
        Integer integer = userService.updateUserStatus(dto.getId(), dto.getStatus());
        if (integer == 1) {
            return ResultVO.ok(Constants.SUCCESS_STATUS, Constants.UPDATE_SUCCESS, null);
        } else {
            return ResultVO.fail(Constants.FAILURE_STATUS, Constants.UPDATE_FAILURE, null);
        }
    }

    @RequestMapping("/login")
    public ResultVO login(@RequestBody UserDTO dto, HttpServletRequest request) {
        UserVO userLogin = userService.login(dto);
        if (userLogin != null) {
            // 保存access_token
            Map<String, Object> map = new HashMap<>();
            String access_token = UUID.randomUUID().toString();
            map.put("access_token", access_token);
            map.put("user_id", userLogin.getId());
            HttpSession session = request.getSession();
            session.setAttribute("access_token", access_token);
            session.setAttribute("user_id", userLogin.getId());
            return ResultVO.ok(Constants.SUCCESS_STATUS, Constants.LOGIN_SUCCESS, map);
        } else {
            return ResultVO.fail(Constants.FAILURE_STATUS, Constants.LOGIN_FAILURE, null);
        }
    }

    @RequestMapping("findUserRoleById/{id}")
    public ResultVO findUserRoleById(@PathVariable Integer id) {
        List<RoleVO> roleVOS = userService.findUserRoleById(id);
        return ResultVO.ok(Constants.SUCCESS_STATUS, Constants.SUCCESS, roleVOS);
    }

    @RequestMapping("/userContextRole")
    public ResultVO userContextRole(@RequestBody UserRoleDTO dto) {
        if (dto.getRoleIdList().size() == userService.userContextRole(dto)) {
            return ResultVO.ok(Constants.SUCCESS_STATUS, Constants.DISTRIBUTION_ROLE_SUCCESS, null);
        } else {
            return ResultVO.ok(Constants.SUCCESS_STATUS, Constants.DISTRIBUTION_ROLE_FAILURE, null);
        }
    }

    @RequestMapping("getUserPermissions")
    public ResultVO getUserPermissions(HttpServletRequest request) {
        //获取请求头中的 token
        String token = request.getHeader("Authorization");
        // 从session中获取token
        HttpSession session = request.getSession();
        String access_token = (String) session.getAttribute("access_token");
        //判断请求头的token和session中的token是否一致
        if (token.equals(access_token)) {
            int user_id = (Integer) session.getAttribute("user_id");
            return ResultVO.ok(Constants.SUCCESS_STATUS, Constants.SUCCESS, userService.getUserPermissions(user_id));
        } else {
            return ResultVO.ok(Constants.FAILURE_STATUS, Constants.GET_MESSAGE_FAILURE, null);
        }
    }
}
